<?php
 include_once("model/dbConfig.php");
 $link =mysql_connect($config['host_address'],$config['username'],$config['password']);
    if (!$link) {
    die('Not connected : username:' .$username. mysql_error());
    }
   mysql_query("set characher set utf8");
      mysql_query("set names utf8");	
		$db_selected = mysql_select_db($config['database'], $link);
    if (!$db_selected)
        {
         die ("Can\'t use menu_item : " . mysql_error());
        }
 
//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$value = json_decode($_POST['json']);
$page=$value->page;
$errors=array();


if($page=='menu'){
	
$rid = $value->rid; // $_POST['rid'];
$url = $value->url;// $_POST['url'];
$label = $value->label;// $_POST['label'];
$toid =$value->toid;// $_POST['toid'];
$detail =$value->detail;// $_POST['detail'];
//flag to indicate which method it uses. If POST set it to 1
 
//Simple server side validation for POST data, of course, 
//you should validate the email

if (!$rid) $errors[count($errors)] = 'Please enter your rid.';
if (!$url) $errors[count($errors)] = 'Please enter your url.'; 
if (!$label) $errors[count($errors)] = 'Please enter your label.'; 
if (!$detail) $errors[count($errors)] = 'Please enter your detail.'; 

   /* $test = '
    			<tr>
		<td>' . $page . '</td>
		<td>' . $rid . '</td>
		<td>' . $url . '</td>
		<td>' . $label . '</td>
		<td>'. $toid . '</td>
		<td>' . nl2br($detail) . '</td>
		</tr>';
		echo $test;*/
//if the errors array is empty, send the mail
if (count($errors)==0) {
    $query="INSERT INTO `mvc`.`menu_item` (`id`, `rid`, `url`, `label`, `toid`, `detail`) VALUES (NULL, '".$rid."', '".$url."', '".$label."', '".$toid."', '".$detail."');";
    $r=executeQuery($query);
    //recipient - change this to your name and email
    //sender
  //  $from = $name . ' <' . $email . '>';
     
    //subject and the html message
    //$subject = 'Comment from ' . $name; 
    if($r==TRUE){
    $id=mysql_insert_id();
    $message = '
    			<tr>
		<td>' . $id . '</td>
		<td>' . $rid . '</td>
		<td>' . $url . '</td>
		<td>' . $label . '</td>
		<td>'. $toid . '</td>
		<td>' . nl2br($detail) . '</td>
		</tr>';
 
    //send the mail
    $result =$message;// sendmail($to, $subject, $message, $from);

      echo $result;   
    
    }else{
    	echo 0;
    }
//if the errors array has values
 } else {
    //display the errors message
    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
    echo '<a href="form.php">Back</a>';
    exit;
 }
		 
}elseif($page=='pages'){
	
	
$rid = $value->rid; // $_POST['rid'];
$name = $value->name;// $_POST['url'];
$imgFolder = $value->imgFolder;// $_POST['label'];
$background =$value->background;// $_POST['toid'];
$detail =$value->detail;// $_POST['detail'];
//flag to indicate which method it uses. If POST set it to 1
 
//Simple server side validation for POST data, of course, 
//you should validate the email

if (!$rid) $errors[count($errors)] = 'Please enter your rid.';
if (!$name) $errors[count($errors)] = 'Please enter your name.'; 
if (!$imgFolder) $errors[count($errors)] = 'Please enter your imgFolder.'; 
if (!$background) $errors[count($errors)] = 'Please enter your background.'; 
if (!$detail) $errors[count($errors)] = 'Please enter your detail.'; 

   /* $test = '
    			<tr>
		<td>' . $page . '</td>
		<td>' . $rid . '</td>
		<td>' . $url . '</td>
		<td>' . $label . '</td>
		<td>'. $toid . '</td>
		<td>' . nl2br($detail) . '</td>
		</tr>';
		echo $test;*/
//if the errors array is empty, send the mail
if (count($errors)==0) {
    $query="INSERT INTO `mvc`.`page_item` (`id`, `rid`, `name`, `imgFolder`, `background`, `detail`) VALUES (NULL, '".$rid."', '".$name."', '".$imgFolder."', '".$background."', '".$detail."');";
    $r=executeQuery($query);
    //recipient - change this to your name and email
    //sender
  //  $from = $name . ' <' . $email . '>';
     
    //subject and the html message
    //$subject = 'Comment from ' . $name; 
    if($r==TRUE){
    $id=mysql_insert_id();
    $message = '
    			<tr>
		<td>' . $id . '</td>
		<td>' . $rid . '</td>
		<td>' . $name . '</td>
		<td>' . $imgFolder . '</td>
		<td>'. $background . '</td>
		<td>' . nl2br($detail) . '</td>
		</tr>';
 
    //send the mail
    $result =$message;// sendmail($to, $subject, $message, $from);

      echo $result;   
    
    }else{
    	echo 0;
    }
//if the errors array has values
 } else {
    //display the errors message
    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
    echo '<a href="form.php">Back</a>';
    exit;
 }
		
}
 
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
	return 0;
	
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";
     
    $result = mail($to,$subject,$message,$headers);
     
    if ($result) return 1;
    else return 0;
}
function executeQuery($query){
		$result=mysql_query($query);

	return $result;
}



?>